package com.songlanyun.modules.oss.controller.admin;

import com.google.gson.Gson;
import com.songlanyun.common.annotation.RepeatSubmit;
import com.songlanyun.common.exception.RRException;
import com.songlanyun.common.utils.*;
import com.songlanyun.common.validator.ValidatorUtils;
import com.songlanyun.common.validator.group.AliyunGroup;
import com.songlanyun.common.validator.group.QcloudGroup;
import com.songlanyun.common.validator.group.QiniuGroup;
import com.songlanyun.modules.account.service.AccountService;
import com.songlanyun.modules.minio.entity.UploadResult;
import com.songlanyun.modules.oss.cloud.CloudStorageConfig;
import com.songlanyun.modules.oss.cloud.OSSFactory;
import com.songlanyun.modules.oss.entity.SysOssEntity;
import com.songlanyun.modules.oss.service.SysOssService;
import com.songlanyun.modules.shop.service.ShopService;
import com.songlanyun.modules.sys.service.SysConfigService;
import com.songlanyun.modules.user.controller.AbstractController;
import com.songlanyun.modules.user.service.SysUserService;
import io.lettuce.core.RedisURI;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Map;


/**
 * 文件上传
 *
 * @author Mark
 * @email sunlightcs@gmail.com
 * @date 2020-11-23 15:35:49
 */
@RestController
@RequestMapping("admin/sys/oss")
public class SysOssAdminController extends AbstractController {
    @Autowired
    private SysOssService sysOssService;
    @Autowired
    private SysConfigService sysConfigService;

    @Resource
    private ShopService shopService;

    private final static String KEY = ConfigConstant.CLOUD_STORAGE_CONFIG_KEY;

    /**
     * 列表
     */
    @GetMapping("/list")
//    @RequiresPermissions("oss:sysoss:save")
    public R list(@RequestParam Map<String, Object> params) {

        PageUtils page = sysOssService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 查询某个商家所使用的内存大小
     */
    @GetMapping("/shopUsed")
//    @RequiresPermissions("oss:sysoss:save")
    public R shopUsed(@RequestParam Map<String, Object> params) {

        Long size = sysOssService.shopUsed(params);

        return R.ok().put("data", size);
    }


    /**
     * 保存
     */
    @PostMapping("/save")
//    @RequiresPermissions("oss:sysoss:save")
    public R save(@RequestBody SysOssEntity sysOss) {
        ValidatorUtils.validateEntity(sysOss);
        sysOss.setShopId(getShopId());
        sysOssService.save(sysOss);

        return R.ok();
    }

    /**
     * 云存储配置信息
     */
//    @GetMapping("/config")
//    @RequiresPermissions("sys:oss:all")
    public R config() {
        CloudStorageConfig config = sysConfigService.getConfigObject(KEY, CloudStorageConfig.class);

        return R.ok().put("config", config);
    }


    /**
     * 保存云存储配置信息
     */
//    @PostMapping("/saveConfig")
//	@RequiresPermissions("sys:oss:all")
    public R saveConfig(@RequestBody CloudStorageConfig config) {
        //校验类型
        ValidatorUtils.validateEntity(config);

        if (config.getType() == Constant.CloudService.QINIU.getValue()) {
            //校验七牛数据
            ValidatorUtils.validateEntity(config, QiniuGroup.class);
        } else if (config.getType() == Constant.CloudService.ALIYUN.getValue()) {
            //校验阿里云数据
            ValidatorUtils.validateEntity(config, AliyunGroup.class);
        } else if (config.getType() == Constant.CloudService.QCLOUD.getValue()) {
            //校验腾讯云数据
            ValidatorUtils.validateEntity(config, QcloudGroup.class);
        }

        sysConfigService.updateValueByKey(KEY, new Gson().toJson(config));

        return R.ok();
    }


    /**
     * 上传文件
     */
    @PostMapping("/upload")
    public R upload(@RequestParam("file") MultipartFile file) throws Exception {

        if (file.isEmpty()) {
            throw new RRException("上传文件不能为空");
        }
        //上传文件
        String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        String url = OSSFactory.build().uploadSuffix(file.getBytes(), suffix);
        String uploadFile = url.substring(url.lastIndexOf("/") + 1);
        //保存文件信息
//        SysOssEntity ossEntity = new SysOssEntity();
//        ossEntity.setUrl(url);
//        sysOssService.save(ossEntity);

        return R.ok().put("data", UploadResult.builder().url(url).filename(uploadFile).build());
    }


    /**
     * 后台系统最上栏显示会员邀请码的二维码
     * 二维码
     *
     * @param
     */
    @GetMapping("/qrcode")
    public R qrcode(@RequestParam("path") String path) {
        //根据商家id获取会员id
        String code = shopService.getCodeByShopId(getShopId(false));
        String url = sysOssService.saveQRcode(path, code);

        return R.ok().put("invitation_code", code).put("data", url);
    }


}
